package com.edt.ichannel.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.dao.MechanismsDao;
import com.edt.ichannel.api.entiy.local.Mechanisms;
import com.edt.ichannel.api.entiy.local.MechanismsCityCode;
import com.edt.ichannel.api.service.MechanismsService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MechanismsServiceImpl implements MechanismsService {
    private Logger logger = LogManager.getLogger(MechanismsServiceImpl.class);

    @Resource
    private MechanismsDao mechanismsDao;
    @Override
    public Mechanisms selectMechanismsById(String id) {
        return mechanismsDao.selectMechanismsById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addMechanismsList(List<Mechanisms> addMechanismsList) {
        if (addMechanismsList != null){
            mechanismsDao.deleteFromDBLink();
            int count = CommonConstant.BRANCH_COUNT;
            int lastIndex = count ;
            for(int index = 0; index < addMechanismsList.size();){
                if(lastIndex > addMechanismsList.size()){
                    lastIndex = addMechanismsList.size();
                    mechanismsDao.addMechanismsList(addMechanismsList.subList(index,lastIndex));
                    break;
                }else{
                    mechanismsDao.addMechanismsList(addMechanismsList.subList(index, lastIndex));
                    index = lastIndex;
                    lastIndex = index + (count);
                }
            }
            logger.info("list 长度："+addMechanismsList.size());
        }else{
            logger.info("list为空");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updatMechanismsList(List<Mechanisms> updateMechanismsList) {
        if (updateMechanismsList != null){
            int count = CommonConstant.BRANCH_COUNT;
            int lastIndex = count ;
            for(int index = 0; index < updateMechanismsList.size();){
                if(lastIndex > updateMechanismsList.size()){
                    lastIndex = updateMechanismsList.size();
                    mechanismsDao.updatMechanismsList(updateMechanismsList.subList(index,lastIndex));
                    break;
                }else{
                    mechanismsDao.updatMechanismsList(updateMechanismsList.subList(index, lastIndex));
                    index = lastIndex;
                    lastIndex = index + (count);
                }
            }
            logger.info("list 长度："+updateMechanismsList.size());
        }else{
            logger.info("list为空");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveOrUpdateMechaismsList(List<Mechanisms> mechanismsList) {

        if(mechanismsList != null && !mechanismsList.isEmpty()){
            List<Mechanisms> addMechanismsList = new ArrayList<>();
            List<Mechanisms> updateMechanismsList = new ArrayList<>();
            for (Mechanisms mechanisms : mechanismsList) {
                if(selectMechanismsById(mechanisms.getId()) != null){
                    updateMechanismsList.add(mechanisms);
                }else{
                    addMechanismsList.add(mechanisms);
                }
            }
            if(!addMechanismsList.isEmpty()){
                addMechanismsList(addMechanismsList);
            }
            if(!updateMechanismsList.isEmpty()){
                updatMechanismsList(updateMechanismsList);
            }

        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateAllCityCode() {
        List<MechanismsCityCode> citys = JSONObject.parseArray(CommonConstant.MECHANISMS_CITYCODES, MechanismsCityCode.class);
        for (MechanismsCityCode city : citys) {
            mechanismsDao.updateCityCode(city.getCityCode(), city.getId());
        }
        List<MechanismsCityCode> mechanismsCityCodes = mechanismsDao.getMechanismsNotCityByParentId(CommonConstant.PARENT_ID_XW, citys);
        for (MechanismsCityCode mCity : mechanismsCityCodes) {
            mechanismsDao.updateCityCode(CommonConstant.PROVINCE_CITY_CODE, mCity.getId());
        }
    }

    @Override
    public void addMechanismsFromStaffDblink() {
        mechanismsDao.addMechanismsFromStaffDblink();
    }

    @Override
    public void mergeMechanismsFromStaffDblink() {
        mechanismsDao.mergeMechanismsFromStaffDblink();
    }

    @Override
    public List<Mechanisms> getMechanismsInfoFromDBLink() {
        return mechanismsDao.getMechanismsInfoFromDBLink();
    }

    @Override
    public void mergeMechanismsFromDblink() {
        mechanismsDao.mergeMechanismsFromDblink();
    }

}
